﻿using System;
using System.Collections.Generic;
using System.Linq;
using ESoft.Core.Data.Service;
using ESoft.Core.Utils.Extensions;
using ESoft.Core.Web.Common.Filters;
using ESoft.Core.Web.VO;
using ESoft.Portal.Data.Domain.Entity;
using Microsoft.AspNetCore.Mvc;

namespace ESoft.Portal.ApiControllers
{
    [Route("api/site/channel")]
    [ApiController]
    [SkipUserAuthorize]
    public class ChannelController : ControllerBase
    {
        private readonly IBaseService _baseService;

        public ChannelController(IBaseService baseService)
        {
            _baseService = baseService;
        }

        [Route("Query")]
        [HttpGet]
        public object Query([FromQuery]string fatherChannelId, [FromQuery]int? isHot, [FromQuery]int? channelType)
        {
            var channelList = _baseService.QueryEntities<CContentChannelEntity>(x =>
                (fatherChannelId.IsNullOrWhiteSpace()||x.FatherChannelId== fatherChannelId)
                && (isHot == null || x.IsHot == isHot)&&(channelType==null||x.ChannelType==channelType)).OrderBy(x=>x.Sort).ThenBy(x=>x.CreateDatetime);
            return ResponseResult.Success(channelList);
        }

        [Route("{channelId}")]
        [HttpGet]
        public object Get([FromRoute]string channelId)
        {
            var channel = _baseService.Find<CContentChannelEntity>(channelId);
            if (channel == null) return null;
            var richText = _baseService.Find<CRichTextEntity>(channel.RichTextId);
            var result = new Dictionary<String, object>
            {
                {"channelId",channel.ChannelId },
                {"channelName",channel.ChannelName },
                {"channelType",channel.ChannelType },
                {"createDatetime",channel.CreateDatetime },
                {"fatherChannelId",channel.FatherChannelId },
                {"imageId",channel.ImageId },
                {"isHot",channel.IsHot },
                {"sort",channel.Sort },
                {"url",channel.Url },
                {"fullDetail", System.Text.Encoding.Default.GetString(richText.TextContent) },
            };
            return ResponseResult.Success(result);
        }
    }
}